//
//  ThemeManagerExtension.swift
//  HeadlineNews
//
//  Created by han on 2019/7/22.
//  Copyright © 2019 han. All rights reserved.
//

import UIKit

// 解析数组
extension ThemeManager {
    
    static func element<T>(for array: [T]) -> T? {
        let index = ThemeManager.currentThemeIndex
        guard  array.indices ~= index else {
            return nil
        }
        return array[index]
    }
    
    static func colorElement(for array: [String]) -> UIColor? {
        guard let rgba = element(for: array) else { return nil }
        guard let color = try? UIColor(rgba_throws: rgba as String) else {
            return nil
        }
        return color
    }
    
    static func imageElement(for array: [String]) -> UIImage? {
        guard let imageName = element(for: array) else { return nil }
        guard let image = UIImage(named: imageName as String) else {
            return nil
        }
        return image
    }
}

// 解析字典
extension ThemeManager {
    static func value(for keyPath: String) -> Any? {
        return currentTheme?.value(forKeyPath: keyPath)
    }
    
    static func string(for keyPath: String) -> String? {
        guard let string = currentTheme?.value(forKeyPath: keyPath) as? String else { return nil }
        return string
    }
    
    static func number(for keyPath: String) -> NSNumber? {
        guard let number = currentTheme?.value(forKeyPath: keyPath) as? NSNumber else { return nil }
        return number
    }
    
    static func dictionary(for keyPath: String) -> NSDictionary? {
        guard let dictionary = currentTheme?.value(forKeyPath: keyPath) as? NSDictionary else { return nil }
        return dictionary
    }
    
    static func color(for keyPath: String) -> UIColor? {
        guard let rgba = string(for: keyPath) else { return nil }
        guard let color = try? UIColor(rgba_throws: rgba) else { return nil }
        return color
    }
    
    static func image(for keyPath: String) -> UIImage? {
        guard let imageName = string(for: keyPath) else { return nil }
        if let filePath = themeConfig?.url?.appendingPathComponent(imageName).path {
            guard let image = UIImage(contentsOfFile: filePath) else { return nil }
            return image
        } else {
            guard let image = UIImage(named: imageName) else { return nil }
            return image
        }
    }
    
    static func image(form value: String?) -> UIImage? {
        guard let imageName = value else { return nil }
        if let filePath = themeConfig?.url?.appendingPathComponent(imageName).path {
            guard let image = UIImage(contentsOfFile: filePath) else { return nil }
            return image
        } else {
            guard let image = UIImage(named: imageName) else { return nil }
            return image
        }
    }
    
    static func color(from value: String?) -> UIColor? {
        guard let rgba = value else { return nil }
        guard let color = try? UIColor(rgba_throws: rgba) else { return nil }
        return color
    }
}



